package com.example.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.test.R;
import com.example.db.DBHelper;

public class Login extends AppCompatActivity implements View.OnClickListener {

    private EditText login_username,login_password;
    private Button btn_login;
    private TextView tv_login_register,tv_login_forget;
    private DBHelper db;
    private SQLiteDatabase sd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        initView();
        getDatabase();
        initEvent();
    }

    private void getDatabase() {
        db = new DBHelper(this);
    }

    private void initEvent() {
        tv_login_register.setOnClickListener(this);
        tv_login_forget.setOnClickListener(this);
        btn_login.setOnClickListener(this);
    }

    private void initView() {
        tv_login_register=findViewById(R.id.tv_login_register);
        tv_login_forget=findViewById(R.id.tv_login_forget);
        btn_login=findViewById(R.id.btn_login);
        login_username=findViewById(R.id.et_login_username);
        login_password=findViewById(R.id.et_login_password);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_login_register:
                Intent in = new Intent(this,Register.class);
                startActivity(in);
                break;
            case R.id.tv_login_forget:
                Intent intent = new Intent(this,Forget.class);
                startActivity(intent);
                break;
            case R.id.btn_login:
                OnMyloginClick();
                break;
                default:
        }
    }

    private void OnMyloginClick() {
        String username = login_username.getText().toString().trim();
        String  password = login_password.getText().toString().trim();
        sd = db.getWritableDatabase();
        Cursor cursor_username = sd.query("student",null,"username=?",
                new String[] {username},null,null,null);
        if(cursor_username.getCount() == 1){
            Cursor cursor = sd.query("student",null,"username=? and password=?",
                    new String[] {username,password},null,null,null);
            if (cursor.getCount() == 1){
                Toast.makeText(Login.this,"登录成功",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(Login.this,Main.class);
                startActivity(intent);
                finish();
            }else {
                Toast.makeText(Login.this,"密码输入错误",Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(Login.this,"该账号还未注册，请先注册",Toast.LENGTH_SHORT).show();
        }
    }


}
